import { getCurrentLocation,getqiforEachProvincesAndCities}  from '../../utils/location.js'
Page({
  data: {
    status:"TO_DO", //跟进状态
    lastName:'',
    firstName:'',
    email:'',
    tel:'',
    province:'',
    city:'',
    qiprovincegoodsarr:[],
    provincechilder:[],
  },
  onLoad: function (options) {
this.db = wx.cloud.database()
this.db.collection('product').doc(options.id).get().then(res=>{
  this.setData({
    product :res.data
  })
  console.log(res.data);
})
// 获取位置
wx.getLocation({
  type: 'wgs84',
  success (res) {
    console.log(res);
  }
 })
// 拿省地区
 const {city,province} = getCurrentLocation()
 const qiprovincegoodsarr =  getqiforEachProvincesAndCities()
 const currentprovincecity = qiprovincegoodsarr.filter(item=>{
   return item.fullname === province
 })
 this.setData({
  city,
  province,
  provincechilder:currentprovincecity[0].childrens,
  qiprovincegoodsarr
 })    
  },
  // 城市
bindCityChange: function(e) {
    const index = e.detail.value
    this.setData({
      city: this.data.provincechilder[index].fullname
    })
  },
  // 省份
bindPickerChange: function(e) {
const index = e.detail.value
  this.setData({
  province: this.data.qiprovincegoodsarr[index].fullname,
  city:this.data.qiprovincegoodsarr[index].childrens[0].fullname,
  provincechilder :this.data.qiprovincegoodsarr[index].childrens
  })
  },
onSubmit(){
    if (this._isFormReady()) {
      this._handsubmit()
    }
},
lastName:'',
    firstName:'',
    email:'',
    tel:'',
    province:'',
    city:'',
_handsubmit(){
  wx.showLoading({
    title: '正在提交',
  })
  setTimeout(function () {
    wx.hideLoading()
  },2000)
  const product = this.data.product._id
  const {city,province,tel,email,firstName,status,lastName} = this.data
  const data ={
    city,province,tel,email,firstName,status,lastName,product
  }
  console.log(data);
  console.log(111); 
wx.cloud.database().collection('test_checkError').add({data}).then(res=>{
  this._showSuccess()
  console.log(res)})
}, 
_showSuccess(){
  wx.showModal({
    title: '预约成功',
    content: '这是一个模态弹窗',
    success () {
      wx.navigateBack({
        delta:1,
      })
    }
  })
},
_isFormReady(){
  const children = this.selectAllComponents('.tesla-input')
  // let count = 0
  children.forEach(item =>{
  // item.checkError()
    // if(item.isReady()) {
    //   count++
    // }
  })
  return true
  // return count === children.length;
}
})